import axios from 'axios'

/**
 * 获取用户信息
 * @returns {Promise} - 返回用户信息
 */
export function getUserInfo() {
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.get('/userService/userController/getUserName', {
    headers: {
      'Authorization': token
    }
  }).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 获取课程信息
 * @param {string|number} scheduleId - 课程安排ID
 * @returns {Promise} - 返回课程信息
 */
export function getCourseInfo(scheduleId) {
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.get('/courseService/courseController/selectCourseInfoByScheduleId', {
    params: {
      scheduleId: scheduleId
    },
    headers: {
      'Authorization': token
    }
  }).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 获取教师信息
 * @param {string|number} courseId - 课程ID
 * @returns {Promise} - 返回教师信息
 */
export function getTeacherInfo(courseId) {
  if (!courseId) {
    return Promise.reject(new Error('课程ID为空'))
  }
  
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  // 确保courseId是字符串类型
  const courseIdStr = String(courseId)
  
  return axios.get('/courseService/courseController/selectTeacherInfo', {
    params: {
      courseId: courseIdStr
    },
    headers: {
      'Authorization': token
    }
  }).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 创建订单
 * @param {Object} orderData - 订单数据
 * @returns {Promise} - 返回创建结果
 */
export function createOrder(orderData) {
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.post(
    '/ordersService/ordersController/payOrders',
    orderData,
    {
      headers: {
        'Authorization': token,
        'Content-Type': 'application/json'
      }
    }
  ).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 查询支付状态
 * @param {string} ordersId - 订单ID
 * @returns {Promise} - 返回支付状态
 */
export function queryPayStatus(ordersId) {
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.get('/ordersService/ordersController/queryPayStatus', {
    params: { ordersId: ordersId },
    headers: {
      'Authorization': token
    }
  }).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 取消预约
 * @param {Object} cancelData - 取消预约数据
 * @returns {Promise} - 返回取消结果
 */
export function cancelBookCourse(cancelData) {
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.post(
    '/courseService/courseController/cancelBookCourse',
    cancelData,
    {
      headers: {
        'Authorization': token
      }
    }
  ).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

/**
 * 获取订单详情
 * @param {string} orderId - 订单ID
 * @returns {Promise} - 返回订单详情
 */
export function getOrderDetail(orderId) {
  if (!orderId) {
    return Promise.reject(new Error('订单ID为空'))
  }
  
  return axios.get(`/orderService/alipayController/queryOrder/${orderId}`)
    .then(response => {
      return response.data
    }).catch(error => {
      throw error
    })
}

/**
 * 退款
 * @param {string} scheduleId - 课程安排ID
 * @returns {Promise} - 返回退款结果
 */
export function refund(scheduleId) {
  if (!scheduleId) {
    return Promise.reject(new Error('课程安排ID为空'))
  }
  
  const token = sessionStorage.getItem('token') || localStorage.getItem('token')
  if (!token) {
    return Promise.reject(new Error('未登录'))
  }
  
  return axios.get('/ordersService/ordersController/refund', {
    params: { scheduleId: scheduleId },
    headers: {
      'Authorization': token
    }
  }).then(response => {
    return response.data
  }).catch(error => {
    throw error
  })
}

// 发送订单支付成功消息
export function sendOrdersPaySuccessMessage(ordersId) {
    const token = sessionStorage.getItem('token') || localStorage.getItem('token')
    if (!token) {
        return Promise.reject(new Error('未登录'))
    }
    
    return axios.get('/ordersService/ordersController/sendOrdersPaySuccessMessage', {
        params: { ordersId },
        headers: {
            'Authorization': token
        }
    })
} 